#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
#
# script_update=2020.08.31
# script_version=r10007
# This var will recover other script's var when source
#
################################################################################################

function show_inex_usage () {
    echo -e "
$script_name $script_version ($script_update)

Usage: $script_cmd [OPTS]

Options:
  -u, --username <username>       Specify username
  -p. --password <password>       Specify password
  --qb <qb_version|no>            Specify which qBittorrent version to be installed
  --de <de_version|no>            Specify which Deluge version to be installed
  --rt <rt_version|no>            Specify which rTorrent version to be installed
  --tr <tr_version|no>            Specify which Transmission version to be installed
  --domain <your_domain>          Configure SSL for you domain via acme.sh
  --hostname  <hostname>          Setup hostname (make no change by default)
  --mt-<single|double|half|max>   How many threads will be used when compiling from source (default max)
  -b <branch>                     Specify which branch of inexistence to be installed (default master)
  -d, --debug                     Enable debug mode
  -s, --skip                      Skip system check, allow you to install this script on unsupported distro
  -y, --yes                       Force yes to all questions
  --enable-ipv6                   Enable IPv6 supoort for rTorrent
  --qb-static                     Install qBittorrent static builds instead of via deb or compile
  --qb-source                     Install qBittorrent by building from source
  --tr-deb                        Install Transmission 2.94 via deb
  -h, --help                      Display this help and exit

  --APPNAME                       Install an application or enable a function/feature

  Available APPNAME:
  vnc | x2go | wine | mono | tools | rclone | flood | flexget | filebrowser | bbr | tweaks | swap

  --no-SOMETHING                  Do not install something or do not do something

  Available SOMETHING:
  flexget | filebrowser | bbr | tweaks | swap | reboot | system-upgrade | source-change

Example:
  $script_cmd -u aniverse -p ajYeTGRkFw42 -y --de 1.3.15 --qb no --rt no  \\
  --hostname myseedbox --flexget --no-bbr --rclone --tweaks --tr-deb --vnc \\
  --no-system-upgrade --no-reboot --no-source-change --no-filebrowser

中文介绍：https://github.com/Aniverse/inexistence#installation-guide
"
    exit
}

################################################################################################

function opts_action () {
    while [ -n "$1" ] ; do case "$1" in
        -u | --user       ) iUser=$2 ; options_validate_username ; shift 2 ;;
        -p | --password   ) iPass=$2 ; options_validate_password ; shift 2 ;;
        -b | --branch     ) iBranch=$2        ; shift 2 ;;
    
        --de              ) de_version=$2     ; shift 2 ;;
        --qb              ) qb_version=$2     ; shift 2 ;;
        --tr              ) tr_version=$2     ; shift 2 ;;
        --rt              ) rt_version=$2     ; shift 2 ;;
        --lt              ) lt_version=$2     ; shift 2 ;;
        --domain          ) rt_domain=$2      ; shift 2 ;;
        --hostname        ) hostname=$2       ; shift 2 ;;
    
        -d | --debug      ) DeBUG=1           ; shift ;;
        -s | --skip       ) SYSTEMCHECK=0     ; shift ;;
        -y | --yes        ) ForceYes=1        ; shift ;;
        -h | --help       ) show_inex_usage   ; shift ;;
        --separate        ) separate=1        ; shift ;;
        --quick           ) quick=1           ; shift ;;
        --qb-static       ) qb_mode=static    ; shift ;;
        --qb-source       ) qb_mode=source    ; shift ;;
        --sihuo           ) sihuo=yes         ; shift ;;
        --eng             ) script_lang=eng   ; shift ;;
        --chs             ) script_lang=chs   ; shift ;;
        --enable-ipv6     ) IPv6Opt=-i        ; shift ;;
    
        --vnc             ) InsVNC="Yes"      ; shift ;;
        --x2go            ) InsX2Go="Yes"     ; shift ;;
        --wine            ) InsWine="Yes"     ; shift ;;
        --mono            ) InsMono="Yes"     ; shift ;;
        --tools           ) InsTools="Yes"    ; shift ;;
        --rclone          ) InsRclone="Yes"   ; shift ;;
        --flood           ) InsFlood="Yes"    ; shift ;;
    
        --swap            ) USESWAP="Yes"     ; shift ;;
        --filebrowser     ) InsFB="Yes"       ; shift ;;
        --flexget         ) InsFlex="Yes"     ; shift ;;
        --tweaks          ) UseTweaks="Yes"   ; shift ;;
        --bbr             ) InsBBR="Yes"      ; shift ;;
    
        --no-swap         ) USESWAP="No"      ; shift ;;
        --no-filebrowser  ) InsFB="No"        ; shift ;;
        --no-flexget      ) InsFlex="No"      ; shift ;;
        --no-tweaks       ) UseTweaks="No"    ; shift ;;
        --no-bbr          ) InsBBR="No"       ; shift ;;
        --no-source-change) aptsources="No"   ; shift ;;
    
        --no-reboot       ) NoReboot=1        ; shift ;;
        --mt-single       ) MAXCPUS=1         ; shift ;;
        --mt-double       ) MAXCPUS=2         ; shift ;;
        --mt-all          ) MAXCPUS=$(nproc)  ; shift ;;
        --mt-half         ) MAXCPUS=$(echo "$(nproc) / 2"|bc)   ; shift ;;
        --tr-deb          ) tr_version=2.94   ; TRdefault=deb   ; shift ;;
        --no-system-upgrade ) skip_system_upgrade=1             ; shift ;;
    
        -- ) shift ; break ;;
    esac ; done
}

function options_validate_username() {
    reserved_names=('adm' 'admin' 'audio' 'backup' 'bin' 'cdrom' 'crontab' 'daemon' 'dialout' 'dip' 'disk' 'fax' 'floppy' 'fuse' 'games' 'gnats' 'irc' 'kmem' 'landscape' 'libuuid' 'list' 'lp' 'mail' 'man' 'messagebus' 'mlocate' 'netdev' 'news' 'nobody' 'nogroup' 'operator' 'none' 'null' 'plugdev' 'proxy' 'root' 'sasl' 'shadow' 'src' 'ssh' 'sshd' 'staff' 'sudo' 'sync' 'sys' 'syslog' 'tape' 'tty' 'users' 'utmp' 'uucp' 'video' 'voice' 'whoopsie' 'www-data')
    count=0
    count=$(echo -n "$iUser" | wc -c)
    if $(echo "${reserved_names[@]}" | grep -wq "$iUser"); then
        echo -e "ERROR: Do not use reversed user name ! 这个用户名不可用，换一个吧"
        exit 1
    elif [[ $count -lt 3 || $count -gt 32 ]]; then
        echo -e "ERROR: User name cannot less than 3 or more than 32 characters ! 用户名长度 3-32 位"
        exit 1
    elif ! [[ "$iUser" =~ ^[a-z][-a-z0-9_]*$ ]]; then
        echo -e "ERROR: Your username must start from a lower case letter and the username"
        echo -e "must contain only lowercase letters, numbers, hyphens, and underscores."
        echo -e "用户名必须由小写字母开头，只能使用小写字母、数字、连字符、下划线"
        exit 1
    fi
}


function options_validate_password() {
    if [ ${#iPass} -lt 8 ]; then                     # At least [8] chars long
        echo -e "ERROR: Password must be at least [8] chars long 密码长度至少要有 8 位"
        exit 1
    elif ! echo "$iPass" | grep -q '[0-9]'; then     # At least [1] number
        echo -e "ERROR: Password must have at least [1] number 密码中必须包含数字"
        exit 1
    elif ! echo "$iPass" | grep -q '[a-zA-Z]'; then  # At least [1] letter
        echo -e "ERROR: Password must have at least [1] letter 密码中必须包含英文字母"
        exit 1
    fi
}


function options_validate_password_quickbox_lite() {
    count=0
    count=$(echo -n "$iPass" | wc -c)
    strength=$(echo "$iPass" | grep -P '(?=^.{8,32}$)(?=^[^\s]*$)(?=.*\d)(?=.*[A-Z])(?=.*[a-z])')
    if [[ $count -lt 8 ]]; then
        echo -e "Your password cannot less than 8 characters !"
        exit 1
    else
        if [[ $strength == "" ]]; then
            echo -e "Your password must consist:"
            echo -e "1. digital numbers"
            echo -e "2. at least one lower case letter"
            echo -e "3. one upper case letter"
            exit 1
        fi
    fi
}
